﻿using AutoMapper;
using B.S.Base.Read.API.Dto;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Diagnostics.Metrics;
using WEBAPI.API.Application.Command;
using WEBAPI.Damoin;
using WEBAPI.ErroeCode;
using WEBAPI.Indaseture.Infasetures;

namespace B.S.Base.Read.API.Application.CommandHandLer
{
    public class GetAccountCommandHandLer : IRequestHandler<GetAccountCommand, APIResult<APIPaing<List<AccountDto>>>>
    {
       private readonly IBaseRepostiy<AccountModel> _accountmodel;
        private readonly IBaseRepostiy<DeptModel> _deptModel;
        private readonly IBaseRepostiy<UserRoleModel> _userRoleModel;
        private readonly IBaseRepostiy<RoleModel> _roleModel;
        public GetAccountCommandHandLer(IBaseRepostiy<AccountModel> accountmodel, IBaseRepostiy<DeptModel> deptModel, IBaseRepostiy<UserRoleModel> userRoleModel, IBaseRepostiy<RoleModel> roleModel)
        {
            this. _accountmodel = accountmodel;
            this. _deptModel = deptModel;
            this. _userRoleModel = userRoleModel;
            this._roleModel = roleModel;

        }
        public async Task<APIResult<APIPaing<List<AccountDto>>>> Handle(GetAccountCommand request, CancellationToken cancellationToken)
        {
            var userdtos = new List<AccountDto>();
            var users = _accountmodel.GetAll().ToList();

            foreach (var user in users)
            {
                var userRole = _userRoleModel.GetAll().FirstOrDefault(x => x.AccountId == user.AccountId);
                int userRoleId = userRole != null ? userRole.UserRoleId : 0;
                int roleId = userRole != null ? userRole.RoleId : 0;
                var dept = _deptModel.GetAll().FirstOrDefault(x => x.DeptId == user.DeptId);
                string deptstring = dept != null ? dept.Deptstring : string.Empty;
                userdtos.Add(new AccountDto
                {   
                    AccountId = user.AccountId,
                    DeptId = user.DeptId,
                    Pwd = user.Pwd,
                    RoleName = RoleName(user.AccountId),
                    AccountName = user.AccountName,
                    Sex = user.Sex,
                    Province = user.Province,
                    Market = user.Market,
                    County = user.County,
                    Address = user.Address,
                    CreateUserTime = user.CreateUserTime,
                    CreateUserName = user.CreateUserName,
                    UpdateUserTime = user.UpdateUserTime,
                    UpdateUserName = user.UpdateUserName,
                    IsDeleted = user.IsDeleted,
                    UserRoleId = userRoleId,
                    RoleId = roleId,
                    Deptstring = deptstring
                });
            }
                var list = userdtos.ToList().Where(x => !x.IsDeleted);
                int totalCount = list.Count();
                int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                //分页
                list = list.OrderByDescending(x => x.AccountId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return new APIResult<APIPaing<List<AccountDto>>>
                {
                    Code = ResultCode.Ok,
                    Data = new APIPaing<List<AccountDto>>
                    {
                        Data = list.ToList(),
                        totalCount = totalCount,
                        totalPage = totalPage,
                    },
                    Message = "获取成功"
                };
            }
        public string RoleName(int id)
        {
            List<int> RoleId = _userRoleModel.GetAll().Where(x => x.AccountId == id).Select(x => x.RoleId).ToList();
            var RoleNames = "";

            foreach (var item in RoleId)
            {
                RoleNames += _roleModel.GetAll().FirstOrDefault(x => x.RoleId == item).RoleName + ',';
            }                     
            return RoleNames.TrimEnd(',');
        }
    }
}

